﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Headsight.BPMN2WF.BPMN.Schemas
{
    /// <summary>
    /// Diversified <i>BPMN extension methods</i>.
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Returns all <para>self's</para> <see cref="tProcess.Items"/> which are instance of
        /// <see cref="tFlowNode"/>.
        /// </summary>
        public static IEnumerable<tFlowNode> GetTFlowNodes(this tProcess self)
        {
            return GetInternal<tFlowNode>(self);
        }

        /// <summary>
        /// Returns all <para>self's</para> <see cref="tProcess.Items"/> which are instance of
        /// <see cref="tSequenceFlow"/>.
        /// </summary>
        public static IEnumerable<tSequenceFlow> GetTSequenceFlows(this tProcess self)
        {
            return GetInternal<tSequenceFlow>(self);
        }

        private static IEnumerable<T> GetInternal<T>(this tProcess self) where T : tFlowElement
        {
            if (self.Items == null || self.Items.Length == 0)
            {
                return new T[0];
            }

            return self.Items.Where(x => typeof (T).IsAssignableFrom(x.GetType())).Cast<T>();
        }

    }
}
